<?php

namespace app\user\controller\mall\order;

use app\user\controller\mall\Base;
use addons\mall\library\Service;

/**
 * 订单投诉
 *
 * @icon fa fa-circle-o
 */
class Complain extends Base
{
    
    /**
     * Complain模型对象
     * @var \app\common\model\mall\order\Complain
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\order\Complain;
        $this->view->assign("complaintypeList", $this->model->getComplaintypeList());
        $this->view->assign("complainstatusList", $this->model->getComplainstatusList());
    }


    public function index() {

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model->alias("c")
                ->join("__MALL_ORDER_ORDER__ o", "c.order_id=o.id", "LEFT")
                ->field("c.*")
                ->where("o.shop_id", cookie('shop_id'))
                ->where($where)
                ->count();

        $list = collection($this->model->alias("c")
                ->join("__MALL_ORDER_ORDER__ o", "c.order_id=o.id", "LEFT")
                ->field("c.*")
                ->where("o.shop_id", cookie('shop_id'))
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select())->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->getComplainDetail(0);
        if (!$row) $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params){
                try{
                    $params['complainStatus'] = 2;
                    $params['respondTime'] = date('Y-m-d H:i:s');

                    //是否采用模型验证
                    if ($this->modelValidate){
                        $name = basename(str_replace("\\model\\", "\\validate\\", get_class($this->model)));
                        $validate = $this->modelSceneValidate ? $name . '.edit' : $name;
                        $result = $this->validate($params,$validate);
                        if(true !== $result){
                            // 验证失败 输出错误信息
                            $this->error($result);
                        }
                    }
                    $order = db('mall_order_order')->alias('o')->join('__USERS__ u','u.id=o.user_id')
                        ->where('o.id',$row['order_id'])
                        ->field('o.orderNo,u.loginName')->find();
                    $result = $row->allowField(true)->isUpdate(true)->save($params);
                    if ($result !== false){
                        $find = ['${ORDER_NO}'];
                        $replace = [$order['orderNo']];
                        $content = Msgtpl::getMsgByTpl('PHONE_ADMIN_COMPLAINT_ORDER', $find, $replace);
                        if ($content != '') {
                            Service::smsAdminNotice($content,'ordercomplain');
                        }

                        $this->success();
                    }
                    else{
                        $this->error($row->getError());
                    }
                }
                catch (\think\exception\PDOException $e){
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->assign("row",$row);
        return $this->view->fetch();
    }

    /**
     * 详情
     */
    public function detail($ids = NULL)
    {
        $row = $this->model->getComplainDetail(0);
        if (!$row) $this->error(__('No Results were found'));
        //获取关联的定单
        $this->assign("row",$row);
        return $this->view->fetch();
    }
}
